# laten we afspreken 4 spaties te gebruiken ipv 1 tab? Is goed

import socket, os, random, time, string
from Config import *
import MessageHandler
import MessageSender
import Logger
import Listener
# import Pinger

def server_tag():
	"""aanmaken van serverTag"""

	pid = os.getpid()
	tag = "T02%s" % str(pid)
	length = 32 - len(tag)
	random.seed(time.time() * pid)
	for i in range(length):
		tag += random.choice(string.letters + string.digits)
	return tag

if __name__ == "__main__":
	# make the socket
	sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
	sock.bind((SERVER_ADDRESS, SERVER_PORT))
    
	# Make the unique server tag
	tag = server_tag()
    
	# Make threads
	logger = Logger.Logger(tag)
	sender = MessageSender.MessageSender(logger, sock)
	handler = MessageHandler.MessageHandler(logger, sender, tag)
	# pinger = Pinger(logger, handler)
	listener = Listener.Listener(handler, logger, sock)
	 
	# start threads
	logger.start()
	sender.start()
	handler.start()
	listener.start()
	# pinger.start()
	sender.add_queue(handler.create_message(601, 0, tag), (CONTROL_SERVER_ADDRESS, CONTROL_SERVER_PORT))
    
	while True:
		#placeholder voor controle dat alle threads nog levend zijn.
		pass
